package cn.oveay.classdesign.controller.servlet;

import cn.oveay.classdesign.bean.basebean.Option;
import cn.oveay.classdesign.bean.basebean.Recode;
import cn.oveay.classdesign.bean.basebean.Subject;
import cn.oveay.classdesign.bean.basebean.User;
import cn.oveay.classdesign.service.RecordService;
import cn.oveay.classdesign.service.SubjectService;
import cn.oveay.classdesign.service.UserService;
import cn.oveay.classdesign.service.impl.RecordServiceImpl;
import cn.oveay.classdesign.service.impl.SubjectServiceImpl;
import cn.oveay.classdesign.service.impl.UserServiceImpl;
import cn.oveay.classdesign.util.commons.Nanoflake;
import cn.oveay.classdesign.util.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/list/vote")
public class VoteServlet extends BaseServlet {
    private SubjectService subjectService = new SubjectServiceImpl();
    private RecordService recordService = new RecordServiceImpl();

    public String forward(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String subjectId = req.getParameter("id");
        Subject subject = subjectService.findById(subjectId);
        List<Option> optionList = subject.getOptions();
        List<Recode> recodes = recordService.findByUserIdANDSubjectId(((User) req.getSession().getAttribute("user")).getId(), subjectId);
        List<String> list = new ArrayList<>();
        if(recodes != null && recodes.size() != 0){
            for(Recode recode : recodes){
                for(Option option : optionList){
                    if(recode.getOptionId().equals(option.getId())){
                        list.add(option.getContent());
                    }
                }
            }
            req.setAttribute("list", list);
        }
        req.setAttribute("subject", subject);
        return "/WEB-INF/pages/vote.jsp";
    }

    public String vote(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String subjectId = req.getParameter("subjectId");
        String[] options = req.getParameterValues("options");
        if(options == null){
            req.setAttribute("error", "必须选中至少一个选项.");
            return "/list/vote?method=forward&id=" + subjectId;
        }
        String userID = ((User) req.getSession().getAttribute("user")).getId();
        if(recordService.findByUserIdANDSubjectId(userID, subjectId).size() != 0){
            req.setAttribute("error", "您已经投过票了.");
            return "/list/vote?method=forward&id=" + subjectId;
        }
        Subject subject = subjectService.findById(subjectId);
        if(subject.getStarttime() > new Date().getTime()){
            req.setAttribute("error", "该投票还未开始.");
            return "/list/vote?method=forward&id=" + subjectId;
        }
        if(subject.getEndtime() < new Date().getTime()){
            req.setAttribute("error", "该投票已经结束.");
            return "/list/vote?method=forward&id=" + subjectId;
        }
        Recode recode = new Recode();
        recode.setSubjectId(subjectId);
        recode.setUserId(userID);
        for(String option : options){
            recode.setId(Nanoflake.getNanoflake());
            recode.setOptionId(option);
            recordService.inseart(recode);
        }
        return "r:/list?method=forward";
    }
}
